'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac040A
    Inherits Snac

    Public Sub New()
        MyBase.New(&H4, &HA)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Dim size As Integer
        For Each x As MissedMessageInfo In _MissedMessageInfos
            size += x.CalculateTotalSize
        Next
        Return size
    End Function

    Private _MissedMessageInfos As New List(Of MissedMessageInfo)
    Public ReadOnly Property MissedMessageInfos() As List(Of MissedMessageInfo)
        Get
            Return _MissedMessageInfos
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer

        index = Snac.SizeFixPart

        Do While index < data.Count
            Dim info As MissedMessageInfo

            info = New MissedMessageInfo
            index += info.Deserialize(index, data)

            _MissedMessageInfos.Add(info)
        Loop

        Me.SetTotalSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function
End Class

Public Class MissedMessageInfo
    Implements ISerializable

    Private _Channel As MessageChannel
    Public Property Channel() As MessageChannel
        Get
            Return _Channel
        End Get
        Set(ByVal value As MessageChannel)
            _Channel = value
        End Set
    End Property

    Private _Uin As String
    Public Property Uin() As String
        Get
            Return _Uin
        End Get
        Set(ByVal value As String)
            _Uin = value
        End Set
    End Property

    Private _WarningLevel As Integer
    Public Property WarningLevel() As Integer
        Get
            Return _WarningLevel
        End Get
        Set(ByVal value As Integer)
            _WarningLevel = value
        End Set
    End Property

    Private _UserClass As New TlvUserClass
    Public ReadOnly Property UserClass() As TlvUserClass
        Get
            Return _UserClass
        End Get
    End Property

    Private _UserStatus As New TlvUserStatus
    Public ReadOnly Property UserStatus() As TlvUserStatus
        Get
            Return _UserStatus
        End Get
    End Property

    Private _OnlineTime As New TlvOnlineTime
    Public ReadOnly Property OnlineTime() As TlvOnlineTime
        Get
            Return _OnlineTime
        End Get
    End Property

    Private _SignOnTime As New TlvSignonTime
    Public ReadOnly Property SignOnTime() As TlvSignonTime
        Get
            Return _SignOnTime
        End Get
    End Property

    Private _MissedReason As MissedMessageReason
    Public Property MissedReason() As MissedMessageReason
        Get
            Return _MissedReason
        End Get
        Set(ByVal value As MissedMessageReason)
            _MissedReason = value
        End Set
    End Property

    Private _MissedMessageCount As Integer
    Public Property MissedMessageCount() As Integer
        Get
            Return _MissedMessageCount
        End Get
        Set(ByVal value As Integer)
            _MissedMessageCount = value
        End Set
    End Property

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return DataSize
        End Get
    End Property

    Private _DataSize As Integer

    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize '_UserClass.TotalSize + _UserStatus.TotalSize + _OnlineTime.TotalSize + _SignOnTime.TotalSize
        End Get
    End Property

    Public Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize
        Throw New NotImplementedException
    End Function

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Throw New NotImplementedException
    End Function

    Private _HasData As Boolean

    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Public Function Deserialize(ByVal offset As Integer, ByVal data As System.Collections.Generic.List(Of Byte)) As Integer
        Deserialize(data.GetRange(offset, data.Count - offset))

        Return DataSize
    End Function

    Public Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        Dim index As Integer

        _Channel = DirectCast(CInt(ByteConverter.ToUInt16(data.GetRange(index, 2))), MessageChannel)
        index += 2

        _Uin = ByteConverter.ToStringFromByteIndex(index, data)
        index += 1 + _Uin.Length

        _WarningLevel = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Dim innerTlvCount As Integer
        Dim innerTlvIndex As Integer

        innerTlvCount = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Do While innerTlvIndex < innerTlvCount
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _UserClass.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _UserStatus.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HF
                    _OnlineTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _SignOnTime.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            innerTlvIndex += 1
            index += desc.TotalSize
        Loop

        _MissedMessageCount = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        _MissedReason = DirectCast(CInt(ByteConverter.ToUInt16(data.GetRange(index, 2))), MissedMessageReason)
        index += 2

        _HasData = True

        _DataSize = index
    End Sub

    Public Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Throw New NotImplementedException
        'Dim data As List(Of Byte)
        'data = New List(Of Byte)

        'data.AddRange(ByteConverter.GetBytes(CUShort(_Channel)))
        'data.Add(_Uin.Length)
        'data.AddRange(ByteConverter.GetBytes(_Uin))
        'data.AddRange(ByteConverter.GetBytes(CUShort(_WarningLevel)))
        'data.AddRange(ByteConverter.GetBytes(CUShort(_WarningLevel)))
    End Function

End Class

Public Enum MissedMessageReason
    MessageInvalid = &H0
    MessageTooLarge = &H1
    MessageRateExceeded = &H2
    SenderToEvil = &H3
    YouAreToEvil = &H4
End Enum